package com.ciwong.epaper.modules.me.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.RefreshNewBookDesk;
import com.ciwong.epaper.modules.msg.util.MessageJumpManager;
import com.ciwong.epaper.ui.BaseFragment;
import com.ciwong.epaper.util.DoubleClickCheckUtils;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.eventbus.EventBus;

/**
 * Created by keepon on 2018/8/6.
 */
public class BookDeskParentFragment extends BaseFragment implements View.OnClickListener {
    private static final int NEW_BOOKDESK = 0;
    private static final int OLD_BOOKDESK = 1;
    private static final String IS_OLD_BOOKDESK_TYPE = "IS_OLD_BOOKDESK_TYPE";

    private FrameLayout mFlCotainer;
    private FragmentManager mChildFragmentManager;
    private FragmentTransaction mFragmentTransaction;
    private ImageView iv_msg;

    private int mCurrentFragmentIndex;

    public static BookDeskParentFragment newInstance() {
        BookDeskParentFragment fragment = new BookDeskParentFragment();
        return fragment;
    }

    public BookDeskParentFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_desk_parent, container,
                false);
        findViews(view);
        initEvent();
        init();
        return view;
    }


    private void findViews(View view) {
        mFlCotainer = view.findViewById(R.id.fl_container);
        iv_msg = view.findViewById(R.id.iv_msg);
    }

    private void initEvent() {
        iv_msg.setOnClickListener(this);

    }

    private void init() {
        mChildFragmentManager = getChildFragmentManager();
        mFragmentTransaction = mChildFragmentManager.beginTransaction();
        mFragmentTransaction.add(R.id.fl_container, NewBookDeskFragment.newInstance());
        mCurrentFragmentIndex = NEW_BOOKDESK;
        mFragmentTransaction.commit();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mCurrentFragmentIndex == NEW_BOOKDESK)
            NewBookDeskFragment.newInstance().setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            EventBus.getDefault().post(new RefreshNewBookDesk(false));
        }
    }

    @Override
    public void onClick(View v) {
        if (!DoubleClickCheckUtils.vertify500Duration()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_msg) {

            MessageJumpManager.jumpToMsgActivity(mActivity, R.string.go_back, RequestCode.HomeWorkFragmentResquestCode.QEQ_FOR_HOME_WORK_MSG);

        }
    }
}
